package com.chujian.Activities;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.Utils.Util;
import com.chujian.model.IncomeMsg;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class IncomeActivity extends Activity{
	private Long mStartTime;
	private Long mEndTime;
	private EditText mEtStartTime;
	private EditText mEtEndTime;
	private TextView mTvShowResult;
	private TextView mTvOrderCount;
	private TextView mTvTotalPrice;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			IncomeMsg imsg=JsonUtil.IncomeMsgFromJson(str);
//			Util.showTextShort(getApplicationContext(), str);
//			mTvShowResult.setText("查询区间内共有订单："+imsg.count+"个，总收入为："+imsg.price+"元");
			mTvOrderCount.setText(imsg.count+"");
			mTvTotalPrice.setText(imsg.price+"元");
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.income);
		initView();
	}
	private void initView() {
		// TODO Auto-generated method stub
		mEtStartTime=(EditText) findViewById(R.id.et_income_start_date);
		mEtEndTime=(EditText) findViewById(R.id.et_income_end_date);
		mTvShowResult=(TextView) findViewById(R.id.tv_income_result);
		mTvOrderCount=(TextView) findViewById(R.id.tv_income_order_count);
		mTvTotalPrice=(TextView) findViewById(R.id.tv_income_total_price);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==2001){
			Long dateTime=data.getLongExtra("dateTime", 0);
			SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
			String time = df.format(new Date(dateTime));
			if(requestCode==1000){
				mStartTime=dateTime;
				mEtStartTime.setText(time);
			}else if(requestCode==1001){
				mEndTime=dateTime;
				mEtEndTime.setText(time);
			}
			
		}
	}
	
	public void doClick(View v){
		Intent intent=new Intent();
		switch (v.getId()) {
		case R.id.btn_income_get_start_date:
			intent.setClass(IncomeActivity.this, IncomeGetDateDialog.class);
			startActivityForResult(intent, 1000);
			break;
		case R.id.btn_income_get_end_date:
			intent.setClass(IncomeActivity.this, IncomeGetDateDialog.class);
			startActivityForResult(intent, 1001);
			break;
		case R.id.btn_income_search:
			if("".equals(mEtStartTime.getText().toString())||"".equals(mEtEndTime.getText().toString())){
				Util.confirmDialog("警告", "请选择开始和结束日期", IncomeActivity.this);
			}else if(mEndTime<mStartTime){
				Util.confirmDialog("警告", "日期不合法，请重新选择日期", IncomeActivity.this);
			}else{
				String url=Constan.BASE_URL+"IncomeServlet";
				Map<String ,Long> map=new HashMap<String, Long>();
				map.put("startTime", mStartTime);
				map.put("endTme", mEndTime);
				Gson gson=new Gson();
				String jsonString=gson.toJson(map);
				new HttpClientPostThread(url, mHandler, jsonString).start();
			}
			break;
		default:
			break;
		}
	}
	
	public void IbBack(View v){
		finish();
	}
}
